var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
Page({
  data: {
    userInfo:[],
    exInfo:[],
    question:[],
    index: 0,
    birthday:'',
    gender:['保密','男','女'],
    gindex:0,
    userimg:'',
    user_thumb:'',
    user_photo:''

  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "账号安全"
    })
    this.getUserInfo();
  },
  bindChangeImg:function(){
    var that = this;
    wx.chooseImage({
      count:1,  //最多可以选择的图片总数  
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        var tempFilePaths = res.tempFilePaths;
        //启动上传等待中...  
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        })
        var uploadImgCount = 0;
          wx.uploadFile({
            url: api.UploadImg,
            filePath: tempFilePaths[0],
            name: 'user_thumb',
            formData: {
              'imgIndex': 0,
              has_code: api.hasCode,
              uptype:1
            },
            header: {
              "Content-Type": "multipart/form-data"
            },
            success: function (res) {
              var data = JSON.parse(res.data);
              if(data.errno==0){
                let image = api.Imgurl + data.data.user_thumb;
                that.setData({
                  userimg: image,
                  user_thumb: data.data.user_thumb,
                  user_photo: data.data.user_photo
                })
              }

            },
            fail: function (res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false,
                success: function (res) { }
              })
            }
          });
      }
    });

  },
  bindDateChange:function(e){
    let that=this;
    let time=e.detail.value
    that.data.userInfo.birthday=time;
    that.setData({
      userInfo:that.data.userInfo,
    })
  },
  bindPickerChange:function(e){
    this.setData({
      gindex: e.detail.value
    })
  },
  bindAnsChange:function(e){
    this.setData({
      index: e.detail.value
    })
  },
  formSubmit:function(e){
    let user_id = wx.getStorageSync('userId');
    var reg = new RegExp('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$');
    var emailVar = reg.test(e.detail.value.email);
    if(!emailVar){
      wx.showToast({
        title:"请输入正确的邮箱格式",
        icon: 'succes',
        duration: 1000,
        mask: true
      });
      return false;
    }  
    let userdata = {
      user_id:user_id,
      birthday: e.detail.value.birthday,
      sex: e.detail.value.gender,
      email: e.detail.value.email,
      wx: e.detail.value.wx,
      qq: e.detail.value.qq,
      parent: e.detail.value.parent,
      mobile_phone: e.detail.value.mobile_phone,
      user_thumb: this.data.user_thumb,
      user_photo: this.data.user_photo
      }
      util.request(api.updateUserInfo, userdata).then(function (res) {
      if (res.errno == 0) {
        let userInfo = wx.getStorageSync('userInfo');
        userInfo.birthday = e.detail.value.birthday;
        userInfo.sex = e.detail.value.gender;
        userInfo.user_thumb = userdata.user_thumb;
        wx.setStorageSync("userInfo", userInfo);
        wx.showToast({
          title:res.data,
          icon: 'succes',
          duration: 1000,
          mask: true
        });
        wx.switchTab({
          url: '/pages/ucenter/index/index',
        });
      }else{
        wx.showToast({
          title: res.data,
          icon: 'succes',
          duration: 1000,
          mask: true
        })
      }
    });

  },
  getUserInfo(){
    let that = this;
    let user_id = wx.getStorageSync('userId');
    let userdata = { user_id: user_id};
    util.request(api.getUserInfo, userdata).then(function (res) {
      if (res.errno == 0) {
        that.setData({
          userInfo: res.data.userinfo,
          exInfo: res.data.extend_info_list,
          question: res.data.passwd_questions,
          gindex: res.data.userinfo.sex,
          userimg: api.Domain +'/images/files/thumbs/'+res.data.userinfo.user_thumb,
          user_thumb: res.data.userinfo.user_thumb
        })
      }
    });
  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  }
})